package jmine.tec.persist.hibernate.listener;

import jmine.tec.utils.debug.TimerGroup;
import jmine.tec.utils.debug.impl.DebugRuntimeFactory;

import org.hibernate.event.LoadEvent;
import org.hibernate.event.def.DefaultLoadEventListener;

/**
 * Times hibernate load events.
 * 
 * @author jrenaut
 */
public class TimedLoadEventListener extends DefaultLoadEventListener {

    public static final String HIBERNATE_TIMER = "Hibernate Timer";

    /**
     * @return the {@link TimerGroup} to be used for hibernate readings
     */
    public static final TimerGroup getHibernateTimer() {
        return DebugRuntimeFactory.getInstance().getOrCreateTimer(HIBERNATE_TIMER);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onLoad(LoadEvent event, LoadType loadType) {
        try {
            getHibernateTimer().start(loadType.getName());
            super.onLoad(event, loadType);
        } finally {
            getHibernateTimer().stop(loadType.getName());
        }
    }

}
